import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.script.Random;
import com.speljohan.rsbot.account.Account;
import com.speljohan.rsbot.script.wrappers.RSInterface;

public class AutoLogin extends Random {

	public static int tries;
	public boolean getBack;
	public String userNameBox;

	public AutoLogin() {
		tries = 0;
		getBack = false;
		userNameBox = RSInterface.getInterface(744).getChild(212).getText();
	}
	
	public String getName() {
		return "Auto Login";
	}

	public String getAuthor() {
		return "Xhamolk";
	}

	public double getVersion() {
		return 2.0;
	}

	public boolean activateCondition() {
		return !isLoggedIn() || getLoginIndex() == 10;
	}

	public int loop() {
		if (isLoggedIn()) {
		    tries = 0;
		    return -1;
	    }
		if (tries == 3) {
			log("Failed Too Much Times, Stopping for Security");
			stopAllScripts();
			return -1;
		}
		if (isLoginScreen()) {
			if (getLoginIndex() == 10) {
				sendText("", true);
				wait(random(300, 1000));
				Account currentAcc = Bot.getAccount();
				String str = currentAcc.getUsername();
				if (!userNameBox.equals(str)) {
					erase();
					sendText(str, false);
				}
				sendText("", true);
				wait(random(300, 600));
				sendText(currentAcc.getPassword(), true);
				tries++;
				getBack = true;
				return random(1000, 1500);
			}
			if (getLoginIndex() == 30) {
				return random(200, 300);
			}
		}
		return random(1300, 3000);
	}

	private void erase() {
		Bot.getInputManager().holdKey(8, random(2000, 3000));
	}
	
}
